// pages/userAppraise/userAppraise.js
import api from '../../utils/api'
const app = getApp()
import {
  upload,
  imgUrl
} from '../../config/config'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: {},
    star: 5,
    uploadImgList: [],
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.globalData.userAppraise)
    this.setData({
      listData: app.globalData.userAppraise
    })
  },
  onShow: function () {

  },
  checkStar(e) {
    // console.log(e)
    let {
      index,
      type
    } = e.currentTarget.dataset
    console.log(index, type)
    if (type === 'g') {
      this.setData({
        star: this.data.star + index + 1
      })
    } else {
      this.setData({
        star: index + 1
      })
    }
  },
  chooseImage() {
    const that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res, 'res=================')
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths[0]
        wx.uploadFile({
          url: upload, //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          success(resp) {
            const data = JSON.parse(resp.data)
            if (data.code * 1 === 0) {
              let url = data.data.url
              that.setData({
                uploadImgList: [...that.data.uploadImgList, url]
              })
            }
            //do something
          },
          fail(err) {
            console.log(err)
          }
        })
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  userCommentCreate() {
    console.log('[userCommentCreate')
    let imgList = []
    this.data.uploadImgList.forEach(item => {
      imgList.push({
        file: item
      })
    })
    if (!this.data.content) {
      wx.showToast({
        title: '请填写评论内容',
        icon: 'none'
      })
      return
    }
    api.userCommentCreate({
      data: {
        vaccineOrderId: this.data.listData.id,
        star: this.data.star,
        content: this.data.content,
        imgList: imgList
      },
      success: res => {
        console.log(res)
        if (res.code * 1 === 0) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }
    })
  },
  bindinput(e) {
    console.log(e)
    this.setData({
      content: e.detail.value
    })
  }
})